package com.chenxi.vhr.utils;

import org.springframework.beans.BeanUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 共同工具类
 * @author : zhangchenxi
 * @create : 2022-02-12
 **/
public class NasUtil {

    /**
     * Java8 新特性
     * list去重
     * @param list
     * @param <T>
     * @return
     */
    public static <T> List<T> distinctList(List<T> list) {
        return list.stream().distinct().collect(Collectors.toList());
    }

    /**
     * 转换称数组
     * @param in
     * @return
     */
    public static String[] asArray(List<String> in)
    {
        if ((in == null) || (in.size() == 0)) {
            return null;
        }
        String[] rtn = new String[in.size()];
        for (int i = 0; i < in.size(); i++) {
            rtn[i] = ((String)in.get(i));
        }
        return rtn;
    }

    public static List<String> asList(String[] ss)
    {
        List rtn = new ArrayList();
        if ((ss != null) && (ss.length > 0)) {
            String[] arrayOfString = ss; int j = ss.length; for (int i = 0; i < j; i++) { String s = arrayOfString[i];
                rtn.add(s);
            }
        }
        return rtn;
    }

    public static boolean inList(List<Integer> list, int i)
    {
        if ((list == null) || (list.size() == 0))
            return false;
        for (Integer ii : list) {
            if (ii.intValue() == i)
                return true;
        }
        return false;
    }

    public static boolean inList(List<String> list, String s)
    {
        if ((list == null) || (list.size() == 0))
            return false;
        if (StringUtil.isNull(s))
            return false;
        for (String ss : list) {
            if (s.equals(ss))
                return true;
        }
        return false;
    }

    /**
     * 获取扩展名
     * @param fileName
     * @return
     */
    public static String getExtensionName(String fileName) {
        if ((fileName.indexOf(".") < 0) && (fileName.startsWith("image%3A"))) {
            return "jpg";
        }
        return fileName.substring(fileName.lastIndexOf(".") + 1);
    }

    public static void copyProperties(Object source, Object target) {
        BeanUtils.copyProperties(source, target);
    }
}
